import Taro from '@tarojs/taro';
// import getBaseUrl from './baseUrl'
import interceptors from './interceptors';
export const BASE_URL = 'https://api.ynylny.com';
 
// export const BASE_URL = "http://3ubqt8478312.vicp.fun";
interceptors.forEach((interceptorItem) => Taro.addInterceptor(interceptorItem));

class httpRequest {
  baseOptions(params, method = 'GET') {
    let { url, data } = params;

    let contentType = 'application/json';
    contentType = params.contentType || contentType;
    3;

    const _token = Taro.getStorageSync('token');
    let _header = {};

    if (_token) {
      _header = {
        Authorization: 'Bearer ' + Taro.getStorageSync('token'),
      };
    }
    const option = {
      url: BASE_URL + url,
      data: data,
      method: method,
      header: {
        'content-type': contentType,
        ..._header,
      },
    };
    return Taro.request(option);
  }

  get(url, data = '') {
    let option = { url, data };
    return this.baseOptions(option, 'GET');
  }

  post(url, data, contentType) {
    let params = { url, data, contentType };
    return this.baseOptions(params, 'POST');
  }

  put(url, data = '') {
    let option = { url, data };
    return this.baseOptions(option, 'PUT');
  }

  delete(url, data = '') {
    let option = { url, data };
    return this.baseOptions(option, 'DELETE');
  }
}

export default new httpRequest();
